package cn.etynit.product.service.impl;

import cn.etynit.basic.util.BaiduAuditUtils;
import cn.etynit.basic.util.LoginContext;
import cn.etynit.basic.util.PageList;
import cn.etynit.org.domain.Employee;
import cn.etynit.org.domain.Shop;
import cn.etynit.org.service.EmployeeService;
import cn.etynit.org.service.IShopService;
import cn.etynit.pet.domain.Pet;
import cn.etynit.product.domain.Product;
import cn.etynit.product.domain.ProductDetail;
import cn.etynit.product.domain.ProductOnlineAuditLog;
import cn.etynit.product.mapper.ProductDetailMapper;
import cn.etynit.product.mapper.ProductMapper;
import cn.etynit.product.mapper.ProductOnlineAuditLogMapper;
import cn.etynit.product.query.ProductQuery;
import cn.etynit.product.service.IProductService;
import cn.etynit.basic.service.impl.BaseServiceImpl;
import cn.etynit.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author etynit
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private IShopService shopService;

    @Override
    public void add(Product product) {
        super.add(product);
        product.getDetail().setProductId(product.getId());
        productDetailMapper.add(product.getDetail());
    }


    @Override
    public Product detail(Long id) {

        return productMapper.detail(id);
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        // 1.我们要过滤掉上架状态的数据，把数据转为map，key是主键，value是对象
        Map<Long, Product> Products = productMapper.findAll().stream().filter(product -> product.getState() == 0).collect(Collectors.toMap(Product::getId, e -> e));
        Date date = new Date();
        for (Long id : ids){
            Product product = Products.get(id);
            StringBuffer sb = new StringBuffer();
            // 不等于null，说明数据本身是下架了的
            if(null !=product) {
                if (!BaiduAuditUtils.TextCensor(product.getName())) {
                    sb.append("服务名称不合规！" + product.getName() + ";");
                    continue;
                }
                if (product.getResources().split(",").length > 1) {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(";")[0])) {
                        sb.append("服务图片不合规！" + product.getResources() + ",");
                    }
                } else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())) {
                        sb.append("服务图片不合规！" + product.getResources() + ";");
                    }
                }
                if (sb.length() > 0) {
                    ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                        productOnlineAuditLog.setNote(sb.toString());
                        productOnlineAuditLog.setAuditTime(date);
                        productOnlineAuditLog.setState(1);
                        productOnlineAuditLog.setAuditId(id);
                        productOnlineAuditLogMapper.add(productOnlineAuditLog);
                    }
                }
            product.setState(1L);
            product.setOnsaletime(date);
            productMapper.update(product);

        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        // 如果要判断上下架状态，全部查询出来，只要状态是下架就不处理
        productMapper.batchOffsale(ids);
    }

    /**
     * 根基服务id查询服务详情
     * @param id
     * @return
     */
    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }

}

